<?php

require_once '../../includes/includes.php';

$error = $_FILES["image"]["error"];

if ($error == UPLOAD_ERR_OK) {

    $title = $_POST['title'];
    $albumId = $_POST['albumId'];

    $albums = getAlbums();
    $albums = indexOf('id', $albums);

    $albumName = $albums[$albumId]['name'];
    $picName = $_FILES["image"]["name"];

    $table = 'photographs';
    $data = array(
        'title' => $title,
        'source' => $picName,
        'album_id' => $albumId
    );

    if ($db->insert($table, $data)) {
        move_uploaded_file($_FILES["image"]["tmp_name"], "../../albums/" . $albumName . '/' . $picName);
    }

    return true;

}

